<template>
  <div class="navigatorContainer" :class="{ isDark: isDark }">
    <div class="item">
      <el-select
        v-model="lang"
        size="small"
        style="width: 100px"
        @change="onLangChange"
      >
        <el-option
          v-for="item in langList"
          :key="item.value"
          :label="item.name"
          :value="item.value"
        />
      </el-select>
    </div>
    <div class="item">
      <el-tooltip
        :effect="isDark ? 'light' : 'dark'"
        :content="$t('navigatorToolbar.backToRoot')"
        placement="top"
      >
        <div class="btn iconfont icondingwei" @click="backToRoot"></div>
      </el-tooltip>
    </div>
    <div class="item">
      <div class="btn iconfont iconsousuo" @click="showSearch"></div>
    </div>
    <div class="item">
      <MouseAction :isDark="isDark" :mindMap="mindMap"></MouseAction>
    </div>
    <div class="item">
      <el-tooltip
        :effect="isDark ? 'light' : 'dark'"
        :content="
          openMiniMap
            ? $t('navigatorToolbar.closeMiniMap')
            : $t('navigatorToolbar.openMiniMap')
        "
        placement="top"
      >
        <div class="btn iconfont icondaohang1" @click="toggleMiniMap"></div>
      </el-tooltip>
    </div>
    <div class="item">
      <el-tooltip
        :effect="isDark ? 'light' : 'dark'"
        :content="
          isReadonly
            ? $t('navigatorToolbar.edit')
            : $t('navigatorToolbar.readonly')
        "
        placement="top"
      >
        <div
          class="btn iconfont"
          :class="[isReadonly ? 'iconyanjing' : 'iconbianji1']"
          @click="readonlyChange"
        ></div>
      </el-tooltip>
    </div>
    <div class="item">
      <Fullscreen :isDark="isDark" :mindMap="mindMap"></Fullscreen>
    </div>
    <div class="item">
    <!--缩放 -->
      <Scale :isDark="isDark" :mindMap="mindMap"></Scale>
    </div>
    <div class="item">
      <div
        class="btn iconfont"
        :class="[isDark ? 'iconmoon_line' : 'iconlieri']"
        @click="toggleDark"
      ></div>
    </div>
<!--    <div class="item">
      <el-dropdown @command="handleCommand">
        <div class="btn iconfont iconbangzhu"></div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="github">Github</el-dropdown-item>
            <el-dropdown-item command="helpDoc">使用文档</el-dropdown-item>
            <el-dropdown-item command="devDoc">开发文档</el-dropdown-item>
            <el-dropdown-item command="site">官方网站</el-dropdown-item>
            <el-dropdown-item command="issue">意见反馈</el-dropdown-item>
            <el-dropdown-item disabled>当前：v{{ version }}</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>-->
  </div>
</template>
<script>
import Scale from './Scale.vue'
import Fullscreen from './Fullscreen.vue'
import MouseAction from './MouseAction.vue'
import { langList } from '@/config/index.js'
import i18n from '@/i18n.js'
import { storeLang, getLang } from '@/api/index.js'
import { mapState, mapActions } from 'pinia'
import pkg from 'simple-mind-map/package.json'
import bus from '@/utils/bus.js'
import { useMindStore } from '@/stores/useMindStore.js'

/**
 * @Author: 王林
 * @Date: 2021-06-24 22:53:10
 * @Desc: 导航器工具栏
 */
export default {
  name: 'NavigatorToolbar',
  components: {
    Scale,
    Fullscreen,
    MouseAction
  },
  props: {
    mindMap: {
      type: Object
    }
  },
  data() {
    return {
      version: pkg.version,
      langList,
      lang: '',
      openMiniMap: false
    }
  },
  computed: {
    ...mapState(useMindStore,['isDark', 'isReadonly'])
  },
  created() {
    this.lang = getLang()
  },
  methods: {
    ...mapActions(useMindStore,['setIsDark', 'setIsReadonly']),

    readonlyChange() {
      this.setIsReadonly(!this.isReadonly)
      this.mindMap.setMode(this.isReadonly ? 'readonly' : 'edit')
    },

    toggleMiniMap() {
      this.openMiniMap = !this.openMiniMap
      bus.emit('toggle_mini_map', this.openMiniMap)
    },

    onLangChange(lang) {
      console.log(lang)
      i18n.global.locale = lang;
      storeLang(lang)
      bus.emit('lang_change')
    },

    showSearch() {
      bus.emit('show_search')
    },

    toggleDark() {
      this.setIsDark(!this.isDark)
    },

    handleCommand(command) {
      let url = ''
      switch (command) {
        case 'github':
          url = 'https://github.com/wanglin2/mind-map'
          break
        case 'helpDoc':
          url = 'https://wanglin2.github.io/mind-map/#/help/zh/'
          break
        case 'devDoc':
          url = 'https://wanglin2.github.io/mind-map/#/doc/zh/introduction/'
          break
        case 'site':
          url = 'https://wanglin2.github.io/mind-map/#/index'
          break
        case 'issue':
          url = 'https://github.com/wanglin2/mind-map/issues/new'
          break
        default:
          break
      }
      const a = document.createElement('a')
      a.href = url
      a.target = '_blank'
      a.click()
    },

    backToRoot() {
      this.mindMap.renderer.setRootNodeCenter()
    }
  }
}
</script>

<style lang="less" scoped>
.navigatorContainer {
  padding: 0 12px;
  position: fixed;
  right: 20px;
  bottom: 20px;
  background: hsla(0, 0%, 100%, 0.8);
  border-radius: 5px;
  opacity: 0.8;
  height: 44px;
  font-size: 12px;
  display: flex;
  align-items: center;

  &.isDark {
    background: #262a2e;

    .item {
      a {
        color: hsla(0, 0%, 100%, 0.6);
      }

      .btn {
        color: hsla(0, 0%, 100%, 0.6);
      }
    }
  }

  .item {
    margin-right: 20px;

    &:last-of-type {
      margin-right: 0;
    }

    a {
      color: #303133;
      text-decoration: none;
    }

    .btn {
      cursor: pointer;
      font-size: 18px;
    }
  }
}

@media screen and (max-width: 590px) {
  .navigatorContainer {
    left: 20px;
    overflow-x: auto;
    overflow-y: hidden;
    height: 60px;
  }
}
</style>
